/*
 * Copyright: (C) 2014 EAST fulinux <fulinux@sina.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <list.h>

struct student{
    int no;
    int age;
    struct dlist_node list;
};

/*
 * 主函数 
 */
int main (int argc, char **argv)
{
    int i;
    struct student *p;
    struct dlist_node head;
    struct dlist_node *iter = NULL;
    struct dlist_node *iter_backup = NULL;

    list_init_head(&head);

    for(i = 50; i; i--){
        p	= (struct student*)malloc(sizeof(struct student));
        if(p==NULL){
            fprintf( stderr, "\ndynamic memory allocation failed\n" );
            exit (EXIT_FAILURE);
        }

        p->no = i;
        p->age = i + 3;
        list_push_back(&head, &p->list);
    }

    list_for_each(iter, &head){
        p = list_entry(iter, struct student, list);
        printf("no = %d, age = %d\n", p->no, p->age);
    }

    printf("Reversed list:\n");
    list_for_each_reverse(iter, &head){
        p = list_entry(iter, struct student, list);
        printf("no = %d, age = %d\n", p->no, p->age);
    }

    list_for_each_remove(iter, iter_backup, &head){
        p = list_entry(iter, struct student, list);
        list_remove_node(iter);
        free(p);
    }

    return 0;
} /* ----- End of main() ----- */
